package cn.lili.controller.payment;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.payment.entity.dos.UserWithdrawApply;
import cn.lili.modules.payment.entity.vo.WithdrawApplyQueryVO;
import cn.lili.modules.wallet.service.WithdrawApplyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 提现记录接口
 *
 * @author pikachu
 * @since 2020/11/16 10:07 下午
 */
@RestController
@Api(tags = "提现接口")
@RequestMapping("/payment/withdraw")
@RequiredArgsConstructor
public class WithdrawApplyController {

    private final WithdrawApplyService withdrawApplyService;


    @ApiOperation(value = "分页获取提现记录")
    @GetMapping
    public ResultMessage<Page<UserWithdrawApply>> getByPage(PageVO page, WithdrawApplyQueryVO withdrawApplyQueryVO) {
        AuthUser currentUser = UserContext.getCurrentUser();
        if (currentUser != null) {
            switch (currentUser.getScene()) {
                case MEMBER -> withdrawApplyQueryVO.setUserId(currentUser.getId());
                case STORE, SUPPLIER, URBAN_SUBSTATION -> withdrawApplyQueryVO.setUserId(UserContext.getCurrentId());
                default -> {
                }
            }
        }
        //构建查询 返回数据
        Page<UserWithdrawApply> memberWithdrawApplyPage = withdrawApplyService.getWithdrawPage(page,
                withdrawApplyQueryVO);
        return ResultUtil.data(memberWithdrawApplyPage);
    }


    @ApiOperation(value = "提现申请")
    @PostMapping("/apply")
    public ResultMessage<Object> apply(Double price) {
        withdrawApplyService.applyWithdrawal(price);
        return ResultUtil.success();
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "提现申请审核")
    @PostMapping("/audit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "审核记录id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "result", value = "审核结果", required = true, paramType = "query", dataType =
                    "boolean"),
            @ApiImplicitParam(name = "remark", value = "审核备注", paramType = "query")
    })
    public ResultMessage<Object> audit(String applyId, Boolean result, String remark) {
        withdrawApplyService.audit(applyId, result, remark);
        return ResultUtil.success();
    }

}
